<template>
  <div v-loading.fullscreen.lock="fullscreenLoading">
     <div>
       <!-- 平台类型 -->
       <third-platform-head
         :defaultPlatformType="999"
         :showKuaiDiNiaoIcon="true"
         :showTmallIcon="false"
         :showKuaiShouIcon="false"
         @platformTypeChange="platformTypeChange">
       </third-platform-head>
    </div>
    <div v-if="platformType === 1">若需要使用<span class="font-orange">抖店电子面单</span>，请先申请开通抖店电子面单服务，<span class="font-blue"><a target="_blank" href="https://fxg.jinritemai.com/index.html#/ffa/mexpress/eBill">去申请</a></span>。</div>
    <div v-if="platformType === 1">打印抖店电子面单需要安装或运行<span class="font-orange">抖店面单打印组件</span>，若未安装过打印组件，<span class="font-blue"><a target="_blank" href="https://logistics.douyinec.com/davinci/index">去下载</a></span>。</div>
    <div v-if="platformType === 3">若需要使用<span class="font-orange">菜鸟电子面单</span>，请先申请开通菜鸟电子面单服务，<span class="font-blue"><a target="_blank" href="https://waybill.wuliu.taobao.com/firstPage.htm">去申请</a></span>。</div>
    <div v-if="platformType === 3">打印菜鸟电子面单需要安装或运行<span class="font-orange">菜鸟面单打印组件</span>，若未安装过打印组件，<span class="font-blue"><a target="_blank" href="https://www.taobao.com/markets/cnwww/print">去下载</a></span>。</div>
    <div v-if="platformType === 4">若需要使用<span class="font-orange">拼多多电子面单</span>，请先申请开通拼多多电子面单服务，<span class="font-blue"><a target="_blank" href="https://mms.pinduoduo.com/logistics/open">去申请</a></span>。</div>
    <div v-if="platformType === 4">打印拼多多电子面单需要安装或运行<span class="font-orange">拼多多面单打印组件</span>，若未安装过打印组件，<span class="font-blue"><a target="_blank" href="http://meta.pinduoduo.com/api/one/app/v1/lateststable?appId=com.xunmeng.pddprint&platform=windows&subType=main">去下载</a></span>。</div>
    <!--顶部搜索 -->
    <div class="top-operate-default mar-t-20">
      <div class="search-item">
        <label>快递公司：</label>
        <el-input v-model="searchQuery.expressName" style="width:200px" placeholder="请输入快递公司名称"></el-input>
        <span v-if="platformType != 999">
          <label class="mar-l-20">店铺：</label>
          <el-select v-model="searchQuery.shopId" placeholder="请选择店铺" class="expend" @change="changeShop">
            <el-option v-for="item in shopList" :key="item.shopId" :label="item.shopName" :value="item.shopId">
            </el-option>
          </el-select>
        </span>
        <el-button-group class="mar-l-20">
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="reset">重置</el-button>
          <el-button v-if="platformType === 999 && addExpressOutletsButton" type="primary" style="margin-left: 10px" @click="addExpressOutlets()">添加快递网点</el-button>
          <el-button v-if="platformType === 999" style="margin-left: 10px" @click="addExpressConfig()">面单对接设置</el-button>
          <el-button v-if="platformType != 999 && synFaceSheetServiceButton"  type="primary" style="margin-left: 10px" @click="synFaceSheet()" :loading="synLoading">同步已开通电子面单服务</el-button>
        </el-button-group>
      </div>
    </div>
    <!-- 内容列表 -->
    <div v-for="(item, i) in expressOutletsList">
      <el-collapse accordion @change="item.isOpen = !item.isOpen" >
        <el-collapse-item>
          <template slot="title">
            <div class="v-align-c mar-b-6 width-per-100 pad-l-4">
              <div>
                <i :class="!item.isOpen ? 'el-icon-circle-plus-outline' : 'el-icon-remove-outline'"></i>
                <span class="mar-l-4 mar-r-20">{{item.enterpriseExpressOutletsList[0].expressName}}</span>
                <el-button v-if="platformType === 999 && allRemoveButton" type="text" @click="batchDeleteExpressOutlets(null,item.enterpriseExpressOutletsList)">移除</el-button>
              </div>
            </div>
          </template>
          <!-- 表格 -->
          <div>
            <el-table  ref="multipleTable" :data="item.enterpriseExpressOutletsList"
                style="width: 100%" header-cell-class-name="default-header" row-class-name="default-row">
              <el-table-column width="200px" label="网点信息" prop="expressType" >
                <template slot-scope="scope">
                  <div>{{ scope.row.sendName }}</div>
                  <div>代码：{{ scope.row.sendSite }}</div>
                </template>
              </el-table-column>
              <el-table-column width="250px" label="发货地址">
                <template slot-scope="scope">
                  <div>
                    {{ scope.row.provinceName + scope.row.cityName +scope.row.districtName }}
                  </div>
                  <div>
                    {{ scope.row.address }}
                  </div>
                </template>
              </el-table-column>
              <el-table-column width="150px" label="业务类型" prop="businessType">
                <template slot-scope="scope">
                  {{ dict.businessType[scope.row.businessType] }}
                </template>
              </el-table-column>
              <el-table-column width="200px" v-if="platformType != 999" label="面单数量" prop="electronicSheetFlag" align="center">
                <template slot-scope="scope">
                  <div>剩余数量：{{ scope.row.availableNum }}</div>
                  <div>已取用数量：{{ scope.row.usedNum }}</div>
                  <div>取消数量：{{ scope.row.cancelNum }}</div>
                  <div>回收数量：{{ scope.row.recoveryNum }}</div>
                </template>
              </el-table-column>
              <el-table-column width="150px" label="默认网点" prop="defaultFlag" align="center">
                <template slot-scope="scope">
                {{scope.row.defaultFlag === 1 ? '是' : '否'}}
                </template>
              </el-table-column>
              <el-table-column width="150px" label="快递公司" prop="expressName" align="center">
                <template slot-scope="scope">
                {{scope.row.expressName}}
                </template>
              </el-table-column>
              <el-table-column min-width="200px" label="操作" align="right" fixed="right" v-if="platformType === 999 && operation">
                <template slot-scope="scope">
                  <el-button type="text" size="medium" @click="editExpressOutlets(scope.row)">编辑</el-button>
                  <el-button type="text" size="medium" @click="batchDeleteExpressOutlets(scope.row,null)">移除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-collapse-item>
      </el-collapse>
    </div>
    <el-dialog title="添加快递网点" :visible.sync="addExpressOutletsVisible" :close-on-press-escape="true" width="800px">
      <el-form :model="expressOutletsEntity" label-position="right" :rules="expressOutletsRules" ref="expressOutletsEntity" label-width="120px" class="pad-30">
        <el-form-item label="快递公司" prop="expressInfo">
          <el-select v-model="expressOutletsEntity.enterpriseExpressId" placeholder="请选择快递公司">
            <el-option v-for="(item, index) in expressEnableList" :key="index" :label="item.expressName" :value="item.enterpriseExpressId"/>
          </el-select>
          <el-select v-model="expressOutletsEntity.businessType" placeholder="请选择业务类型">
            <el-option v-for="(val, key) in dict.businessType" :key="key" :label="val" :value="key"/>
          </el-select>
        </el-form-item>
        <el-form-item label="网点名称" prop="sendName">
          <div>
            <el-input v-model="expressOutletsEntity.sendName" placeholder="请输入网点名称" max="64"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="网点标识" prop="sendSite">
          <div><el-input v-model="expressOutletsEntity.sendSite" placeholder="请输入网点标识" max="10"></el-input></div>
        </el-form-item>
        <el-form-item label="发货地址" prop="allAddress">
          <div>
            <el-select v-model="expressOutletsEntity.provinceCode" placeholder="省" @change="changeProvince(expressOutletsEntity.provinceCode)">
              <el-option v-for="(item, index) in provinceList" :key="index" :label="item.areaName" :value="item.areaCode" />
            </el-select>
            <el-select v-model="expressOutletsEntity.cityCode" placeholder="市" @change="changeCity(expressOutletsEntity.cityCode)" >
              <el-option v-for="(item, index) in cityList" :key="index" :label="item.areaName" :value="item.areaCode" />
            </el-select>
            <el-select v-model="expressOutletsEntity.districtCode" placeholder="区" >
              <el-option v-for="(item, index) in districtList" :key="index" :label="item.areaName" :value="item.areaCode" />
            </el-select>
            <div><el-input v-model="expressOutletsEntity.address" class="mar-t-10" placeholder="请输入发货地址" max="64"></el-input></div>
          </div>
        </el-form-item>
        <el-form-item label="客户号" prop="customerName">
          <div><el-input v-model="expressOutletsEntity.customerName" placeholder="请输入客户号" max="11"></el-input></div>
        </el-form-item>
        <el-form-item label="客户密码" prop="customerPwd">
          <div><el-input v-model="expressOutletsEntity.customerPwd" placeholder="请输入客户密码" max="10"></el-input></div>
        </el-form-item>
        <el-form-item label="月结号" prop="monthCode">
          <div><el-input v-model="expressOutletsEntity.monthCode" placeholder="请输入月结号" max="11"></el-input></div>
        </el-form-item>
        <el-form-item label="快递员" prop="sendStaff">
          <div><el-input v-model="expressOutletsEntity.sendStaff" placeholder="请输入快递员" max="10"></el-input></div>
        </el-form-item>
        <el-form-item label="邮费支付方式" prop="payType">
          <div>
            <el-select v-model="expressOutletsEntity.payType" placeholder="请选择邮费支付方式">
              <el-option v-for="(val, key) in dict.payType" :key="key" :label="val" :value="key"/>
            </el-select>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addExpressOutletsVisible = false">取消</el-button>
        <el-button type="primary" @click="saveEnterpriseExpressOutlets">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="面单对接设置" :visible.sync="expressConfigVisible" :close-on-press-escape="true" width="800px">
      <el-form :model="expressConfig" label-position="right" label-width="120px" :rules="rules" ref="expressConfig" class="pad-30">
        <el-form-item label="商户ID" prop="businessId">
          <div><el-input v-model="expressConfig.businessId" placeholder="请输入向快递鸟申请的商户ID" max="64"></el-input></div>
        </el-form-item>
        <el-form-item label="AppKey" prop="appKey">
          <div>
            <el-input v-model="expressConfig.appKey" placeholder="请输入向快递鸟申请的AppKey" max="64"></el-input>
            <span class="font-12">企业用户可登录快递鸟官网进行注册申请客户ID及AppKey</span>
          </div>
        </el-form-item>
        <el-form-item label="发件人" prop="senderName">
          <div><el-input v-model="expressConfig.senderName" placeholder="请输入发件人名称" max="10"></el-input></div>
        </el-form-item>
        <el-form-item label="发件人手机号" prop="senderPhone">
          <div><el-input v-model="expressConfig.senderPhone" placeholder="请输入发件人手机号" max="11"></el-input></div>
        </el-form-item>
        <el-form-item label="发件人地址" prop="senderAddress">
          <div>
            <el-select v-model="expressConfig.provinceCode" placeholder="省" @change="changeProvince(expressConfig.provinceCode)">
              <el-option v-for="(item, index) in provinceList" :key="index" :label="item.areaName" :value="item.areaCode" />
            </el-select>
            <el-select v-model="expressConfig.cityCode" placeholder="市" @change="changeCity(expressConfig.cityCode)" >
              <el-option v-for="(item, index) in cityList" :key="index" :label="item.areaName" :value="item.areaCode" />
            </el-select>
            <el-select v-model="expressConfig.districtCode" placeholder="区">
              <el-option v-for="(item, index) in districtList" :key="index" :label="item.areaName" :value="item.areaCode" />
            </el-select>
            <div><el-input v-model="expressConfig.senderAddress" class="mar-t-10" placeholder="请输入发件人地址" max="64"></el-input></div>
          </div>
        </el-form-item>
        <el-form-item label="发件人邮政编码" prop="senderZipCode">
          <div><el-input v-model="expressConfig.senderZipCode" placeholder="请输入发件人邮政编码" max="10"></el-input></div>
          <span class="font-12">对接EMS、邮政国内标快、邮政快递包裹电子面单需要邮政编码。</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="expressConfigVisible = false">取消</el-button>
        <el-button type="primary" @click="saveEnterpriseExpressConfig">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import thirdPlatformHead from '../../../../shop/components/third-platform-head'
  import { saveEnterpriseExpressConfig, saveExpress, listExpress, getEnterpriseExpressConfig } from '@/api/goods/express'
  import { listProvince, listCity, listDistrict } from '@/api/area/area'
  import { listThirdShop } from '@/api/user/third-shop'
  import { saveEnterpriseExpressOutlets, batchDeleteExpressOutlets, synFaceSheet, listExpressOutlets } from '@/api/goods/expressOutlets'
  import { getDictByGroupName } from '@/api/common/common'
  export default {
    props: {
      showFlag: {
        type: String
      },
      villageSupplierConfig: {
        type: Object,
        default: null
      }
    },
    components: { thirdPlatformHead },
    data() {
      // 自定义地址验证规则
      var addressRule = (rule, value, callback) => {
        if (!this.expressOutletsEntity.provinceCode) {
          return callback(new Error('省不能为空！'))
        } else if (!this.expressOutletsEntity.cityCode) {
          return callback(new Error('市不能为空！'))
        } else if (!this.expressOutletsEntity.districtCode) {
          return callback(new Error('区不能为空！'))
        } else if (!this.expressOutletsEntity.address) {
          return callback(new Error('详细地址不能为空！'))
        } else {
          return callback()
        }
      }
      var expressInfoRule = (rule, value, callback) => {
        if (!this.expressOutletsEntity.enterpriseExpressId) {
          return callback(new Error('请选择快递公司！'))
        } else if (!this.expressOutletsEntity.businessType) {
          return callback(new Error('请选择业务类型！'))
        } else {
          return callback()
        }
      }
      return {
        fullscreenLoading: false,
        // 快递鸟同步面单信息
        synKdnLoading: false,
        synLoading: false,
        // 添加快递网点
        addExpressOutletsVisible: false,
        expressConfigVisible: false,
        expressSheetVisible: false,
        // 企业启用的快递
        expressEnableList: [],
        // 平台类型
        platformType: 999,
        // 电子面单查询参数
        searchQuery: {
          expressName: '',
          shopId: ''
        },
        // 省集合
        provinceList: [],
        // 市集合
        cityList: [],
        // 区集合
        districtList: [],
        // 面单对接设置
        expressConfig: {
          configId: '',
          enterpriseId: '',
          supplierId: '',
          businessId: '',
          appKey: '',
          senderName: '',
          senderPhone: '',
          provinceCode: '',
          cityCode: '',
          districtCode: '',
          senderAddress: '',
          senderZipCode: '',
          homeAreaFilter: '',
          minLogisticsFee: ''
        },
        expressOutletsRules: {
          expressInfo: [
            { required: true, validator: expressInfoRule, trigger: 'blur' }
          ],
          sendName: [
            { required: true, message: '请填写网点名称', trigger: 'blur' }
          ],
          sendSite: [
            { required: true, message: '请填写网点标识', trigger: 'blur' }
          ],
          allAddress: [
            { required: true, validator: addressRule, trigger: 'blur' }
          ],
          payType: [
            { required: true, message: '请选择邮费支付方式', trigger: 'change' }
          ]
        },
        dict: {
          payType: {},
          expressType: {},
          businessType: {}
        },
        rules: {
          businessId: [
            { required: true, message: '请填写商户ID', trigger: 'blur' }
          ],
          appKey: [
            { required: true, message: '请填写appKey', trigger: 'blur' }
          ]
        },
        // 物流网点entity
        expressOutletsEntity: {
          enterpriseExpressOutletsId: '',
          enterpriseExpressId: '',
          businessType: '',
          platformType: '',
          provinceCode: '',
          cityCode: '',
          districtCode: '',
          address: '',
          sendName: '',
          customerName: '',
          customerPwd: '',
          monthCode: '',
          sendSite: '',
          sendStaff: '',
          payType: ''
        },
        // 店铺集合
        shopList: [],
        // 快递网点表格信息
        expressOutletsList: [],
        // 添加快递鸟快递网点按钮
        addExpressOutletsButton: true,
        // 同步已开通电子面单服务按钮
        synFaceSheetServiceButton: true,
        allRemoveButton: true,
        operation: true,
        // 面单使用数据类型：1-企业数据；2-供应商数据
        kuaiDiNiaoUseType: '',
        taoBaoUseType: '',
        douYinUseType: '',
        pinDuoDuoUseType: ''
      }
    },
    // 初始化页面
    created() {
      if (this.getSupplierId()) {
        const villageSupplierConfig = this.villageSupplierConfig
        if (!villageSupplierConfig) {
          this.$message({ type: 'error', message: '获取供应商配置错误！' })
          return
        }
        this.kuaiDiNiaoUseType = villageSupplierConfig.kuaiDiNiaoUseType
        this.taoBaoUseType = villageSupplierConfig.taoBaoUseType
        this.douYinUseType = villageSupplierConfig.douYinUseType
        this.pinDuoDuoUseType = villageSupplierConfig.pinDuoDuoUseType
        if (this.kuaiDiNiaoUseType === 1) {
          this.addExpressOutletsButton = false
          this.allRemoveButton = false
          this.operation = false
        }
      }
      this.init()
      this.initDict()
    },
    methods: {
      // 初始数据
      init() {
        const initFx = new Promise((resolve, reject) => {
          if (this.platformType !== 999) {
            this.initShop(resolve, reject)
          } else {
            resolve('success')
          }
        })
        initFx.catch((result) => {
          this.$message({ type: 'error', message: result })
          return
        })
        initFx.then((result) => {
          if (result === 'success') {
            this.listExpressOutlets()
          }
        })
      },
      // 非快递鸟平台初始化店铺信息
      initShop(resolve, reject) {
        this.searchQuery.shopId = ''
        this.shopList = []
        const params = {
          enterpriseId: this.getEnterpriseId(),
          supplierId: this.getSupplierId(),
          platformType: this.platformType
        }
        if (this.platformType === 1 && this.douYinUseType === 1) {
          params.supplierId = ''
        }
        if (this.platformType === 3 && this.taoBaoUseType === 1) {
          params.supplierId = ''
        }
        if (this.platformType === 4 && this.pinDuoDuoUseType === 1) {
          params.supplierId = ''
        }
        this.fullscreenLoading = true
        listThirdShop(params).then(response => {
          if (response.isError) return
          if (response.data) {
            if (response.data.length > 0) {
              this.searchQuery.shopId = response.data[0] && response.data[0].shopId
              this.shopList = response.data
              resolve('success')
            } else {
              this.fullscreenLoading = false
              reject('平台下不存在店铺，请检查！')
            }
          }
        })
      },
      // 初始化数据字典
      initDict() {
        getDictByGroupName('EnterpriseExpressOutletsDTO').then(response => {
          this.dict.businessType = response.data.businessType
        })
        getDictByGroupName('EnterpriseExpressDTO').then(response => {
          this.dict.payType = response.data.payType
        })
      },
      // 平台类型切换事件
      platformTypeChange(platformType) {
        this.platformType = platformType
        this.searchQuery.expressName = ''
        this.expressOutletsList = []
        // 使用企业打单时关闭相关按钮
        if (this.platformType === 999) {
          if (this.kuaiDiNiaoUseType === 1 && this.getSupplierId()) {
            this.addExpressOutletsButton = false
            this.allRemoveButton = false
            this.operation = false
          }
        } else {
          if (this.platformType === 1 && this.douYinUseType === 1 && this.getSupplierId()) {
            this.synFaceSheetServiceButton = false
          }
          if (this.platformType === 3 && this.taoBaoUseType === 1 && this.getSupplierId()) {
            this.synFaceSheetServiceButton = false
          }
          if (this.platformType === 4 && this.pinDuoDuoUseType === 1 && this.getSupplierId()) {
            this.synFaceSheetServiceButton = false
          }
        }
        this.init()
      },
      // 网点列表查询
      listExpressOutlets() {
        // 搜索状态
        this.fullscreenLoading = true
        this.expressOutletsList = []
        this.searchQuery.enterpriseId = this.getEnterpriseId()
        this.searchQuery.supplierId = this.getSupplierId()
        const platformType = this.platformType
        if (platformType === 999) {
          this.searchQuery.shopId = ''
          if (this.kuaiDiNiaoUseType === 1) {
            this.searchQuery.supplierId = ''
          }
        }
        if ((platformType === 1 && this.douYinUseType === 1) || (platformType === 3 && this.taoBaoUseType === 1) || (platformType === 4 && this.pinDuoDuoUseType === 1)) {
          this.searchQuery.supplierId = ''
        }
        this.searchQuery.platformType = platformType
        listExpressOutlets(this.searchQuery).then(response => {
          if (response.isError) return
          const dataList = response.data
          dataList.forEach(e => {
            e.isOpen = false
          })
          this.expressOutletsList = dataList
          this.fullscreenLoading = false
        })
      },
      // 店铺选择事件
      changeShop() {
        this.listExpressOutlets()
      },
      saveExpressSheet() {
        this.dialogExpress.enterpriseId = this.getEnterpriseId()
        saveExpress(this.dialogExpress).then(response => {
          if (response.isError) return
          this.expressSheetVisible = false
          this.$message({ type: 'success', message: '保存成功！' })
          this.init()
        })
      },
      // 同步电子面单服务
      synFaceSheet() {
        const shopId = this.searchQuery.shopId
        if (this.platformType !== 999) {
          if (!shopId) {
            this.$message({
              type: 'error',
              message: '请选择店铺!'
            })
            return
          }
        }
        this.synKdnLoading = true
        this.synLoading = true
        const params = {
          enterpriseId: this.getEnterpriseId(),
          supplierId: this.getSupplierId(),
          platformType: this.platformType,
          shopId: shopId
        }
        synFaceSheet(params).then(response => {
          this.synKdnLoading = false
          this.synLoading = false
          if (response.isError) return
          this.init()
        })
      },
      changeProvince(provinceCode) {
        if (!provinceCode) {
          return
        }
        this.expressOutletsEntity.cityCode = ''
        this.expressOutletsEntity.districtCode = ''
        this.expressConfig.cityCode = ''
        this.expressConfig.districtCode = ''
        this.getCityList(provinceCode)
      },
      changeCity(cityCode) {
        if (!cityCode) {
          return
        }
        this.expressOutletsEntity.districtCode = ''
        this.expressConfig.districtCode = ''
        this.getDistrictList(cityCode)
      },
      getProvinceList() {
        listProvince().then(response => {
          if (response.isError) return
          this.provinceList = response.data
        })
      },
      getCityList(provinceCode) {
        listCity(provinceCode).then(response => {
          if (response.isError) return
          this.cityList = response.data
        })
      },
      getDistrictList(cityCode) {
        listDistrict(cityCode).then(response => {
          if (response.isError) return
          this.districtList = response.data
        })
      },
      // 点击面单对接设置
      addExpressConfig() {
        this.expressConfigVisible = true
        this.getEnterpriseExpressConfig()
        this.$refs['expressConfig'].clearValidate()
      },
      // 获取面单对接设置数据
      getEnterpriseExpressConfig() {
        getEnterpriseExpressConfig(this.getEnterpriseId(), this.getSupplierId()).then(response => {
          if (response.isError) return
          this.expressConfig = Object.assign(this.expressConfig, response.data)
          this.getProvinceList()
          if (this.expressConfig.provinceCode) {
            this.getCityList(this.expressConfig.provinceCode)
          }
          if (this.expressConfig.cityCode) {
            this.getDistrictList(this.expressConfig.cityCode)
          }
          if (!this.expressConfig.homeAreaFilter) {
            this.expressConfig.homeAreaFilter = ''
          }
          if (!this.expressConfig.minLogisticsFee) {
            this.expressConfig.minLogisticsFee = ''
          }
        })
      },
      // 保存快递网点
      saveEnterpriseExpressOutlets() {
        this.$refs['expressOutletsEntity'].validate((valid) => {
          if (valid) {
            this.expressOutletsEntity.platformType = this.platformType
            this.expressOutletsEntity.enterpriseId = this.getEnterpriseId()
            this.expressOutletsEntity.supplierId = this.getSupplierId()
            saveEnterpriseExpressOutlets(this.expressOutletsEntity).then(response => {
              if (response.isError) return
              this.addExpressOutletsVisible = false
              this.$message({ type: 'success', message: '保存成功！' })
              this.init()
            })
          }
        })
      },
      // 添加快递鸟快递网点
      addExpressOutlets() {
        this.addExpressOutletsVisible = true
        this.cityList = []
        this.districtList = []
        this.getProvinceList()
        this.getExpressList()
        Object.assign(this.$data.expressOutletsEntity, this.$options.data().expressOutletsEntity)
        this.$refs['expressOutletsEntity'].clearValidate()
      },
      // 编辑快递鸟快递网点
      editExpressOutlets(row) {
        this.addExpressOutletsVisible = true
        Object.assign(this.expressOutletsEntity, row)
        this.expressOutletsEntity.businessType = row.businessType.toString()
        this.expressOutletsEntity.payType = row.payType.toString()
        this.getExpressList()
        this.getProvinceList()
        this.getCityList(this.expressOutletsEntity.provinceCode)
        this.getDistrictList(this.expressOutletsEntity.cityCode)
      },
      // 获取快递列表
      getExpressList() {
        // 获取物流信息
        listExpress({ enterpriseId: this.getEnterpriseId(), expressStatus: 1, supplierId: this.getSupplierId() }).then(response => {
          if (response.isError) return
          this.expressEnableList = response.data
        })
      },
      // 保存面单对接设置
      saveEnterpriseExpressConfig() {
        if (!this.expressConfig.homeAreaFilter) {
          this.expressConfig.homeAreaFilter = ''
        }
        if (!this.expressConfig.minLogisticsFee) {
          this.expressConfig.minLogisticsFee = ''
        }
        if (this.activeName === 'logistics' && this.expressConfig.minLogisticsFee * 1 <= 0) {
          this.$message({ type: 'error', message: '最低物流费必须大于0！' })
          return
        }
        this.$refs['expressConfig'].validate((valid) => {
          if (valid) {
            this.expressConfig.enterpriseId = this.getEnterpriseId()
            this.expressConfig.supplierId = this.getSupplierId()
            saveEnterpriseExpressConfig(this.expressConfig).then(response => {
              if (response.isError) return
              this.$message({ type: 'success', message: '保存成功！' })
              this.expressConfigVisible = false
            })
          }
        })
      },
      // 批量删除物流网点
      batchDeleteExpressOutlets(row, data) {
        var ids = []
        if (row) {
          ids.push(row.enterpriseExpressOutletsId)
        } else {
          if (data && data.length > 0) {
            data.forEach(o => {
              ids.push(o.enterpriseExpressOutletsId)
            })
          }
        }
        if (ids.length === 0) {
          this.$message({ message: '暂无快递网点', type: 'warning' })
          return
        }
        this.$confirm('快递网点删除后不可恢复，确定要删除？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          batchDeleteExpressOutlets({ enterpriseExpressOutletsIds: ids }).then(response => {
            if (response.isError) return
            this.$message({ type: 'success', message: '删除成功!' })
            this.allExpressOutletsChecked = false
            this.init()
          })
        }).catch(() => {
        })
      },
      handleSearch() {
        this.init()
      },
      reset() {
        this.searchQuery.expressName = ''
        this.init()
      }
    }
  }
</script>

<style lang="less" scoped>
</style>
